#!/usr/bin/env ruby

LKP_SRC = ENV['LKP_SRC'] || File.dirname(File.dirname(File.realpath($PROGRAM_NAME)))
RESULT_ROOT = ENV['RESULT_ROOT']

require "#{LKP_SRC}/lib/tests/stats"

stats = LKP::Stats.new

memleaks = []
memleak = {}
bts = ""

while (line = $stdin.gets)
  case line.chomp!
  when /^unreferenced object/ # unreferenced object 0xffff9375a9b84e00 (size 128):
    if !bts.empty? && memleak['type']
      memleak['bt'] = bts
      memleaks << memleak
    end
    bts = ''
    memleak['type'] = 'unreferenced_object'
  when /comm "(.*)"/ # comm "swapper/0", pid 1, jiffies 4294667990 (age 37.508s)
    memleak['comm'] = $1
#    [<(____ptrval____)>] unpack_to_rootfs+0x3d/0x304
#    [<(____ptrval____)>] populate_rootfs+0x19/0x106
  when /\[.*\] (.*)\+/
    bts = ".#{$1}#{bts}"
  end
end

if !bts.empty? && memleak['type']
  memleak['bt'] = bts
  memleaks << memleak
end

memleaks.uniq.each { |m| stats.add "#{m['comm']}.#{m['type']}#{m['bt']}", 1 }

stats.dump
